﻿namespace SIEMENS
{
    /// <summary>
    /// PLC方法
    /// </summary>
    public class PLCMethods
    {
        private readonly PlcCommunicator _plcCommunicator;

        public PLCMethods(PlcCommunicator plcCommunicator)
        {
            this._plcCommunicator = plcCommunicator;
        }

        #region Word类型方法

        /// <summary>
        /// 直流控制电源电压值
        /// </summary>
        public bool WriteDC(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW2", value);
        }

        /// <summary>
        /// 交流电源电压值
        /// </summary>
        public bool WriteAC(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW4", value);
        }

        /// <summary>
        /// 交流电源频率值
        /// </summary>
        public bool WriteACRate(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW6", value);
        }

        /// <summary>
        /// 直流电机电源电压值
        /// </summary>
        public bool WriteDCVoltage(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW8", value);
        }

        /// <summary>
        /// t时间
        /// </summary>
        public bool WriteT(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW12", value);
        }

        /// <summary>
        /// t1时间
        /// </summary>
        public bool WriteT1(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW16", value);
        }

        /// <summary>
        /// 合闸命令时间
        /// </summary>
        public bool WriteCloseTime(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW20", value);
        }

        /// <summary>
        /// 分闸命令时间
        /// </summary>
        public bool WriteOpenTime(ushort value)
        {
            return _plcCommunicator.WriteData("DB13.DBW24", value);
        }

        #endregion

        #region 新增的Bool类型命令方法

        /// <summary>
        /// 选直流控制电源
        /// </summary>
        public bool SelectDCPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.0", value);
        }

        /// <summary>
        /// 选交流控制电源
        /// </summary>
        public bool SelectACPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.1", value);
        }

        /// <summary>
        /// 选外接控制电源
        /// </summary>
        public bool SelectExternalPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.2", value);
        }

        /// <summary>
        /// 选直流电机电源
        /// </summary>
        public bool SelectDCMotorPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.3", value);
        }

        /// <summary>
        /// 选交流电机电源
        /// </summary>
        public bool SelectACMotorPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.4", value);
        }

        /// <summary>
        /// 选外接电机电源
        /// </summary>
        public bool SelectExternalMotorPower(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.5", value);
        }

        /// <summary>
        /// 电源指令on/off
        /// </summary>
        public bool PowerCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.6", value);
        }

        /// <summary>
        /// 电机指令on/off
        /// </summary>
        public bool MotorCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX0.7", value);
        }

        /// <summary>
        /// 选A相
        /// </summary>
        public bool SelectPhaseA(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.0", value);
        }

        /// <summary>
        /// 选B相
        /// </summary>
        public bool SelectPhaseB(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.1", value);
        }

        /// <summary>
        /// 选C相
        /// </summary>
        public bool SelectPhaseC(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.2", value);
        }

        /// <summary>
        /// 选ABC相
        /// </summary>
        public bool SelectAllPhases(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.3", value);
        }

        /// <summary>
        /// 选二次线圈
        /// </summary>
        public bool SelectSecondaryCoil(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.4", value);
        }

        /// <summary>
        /// 合闸命令
        /// </summary>
        public bool CloseCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.5", value);
        }

        /// <summary>
        /// 分闸命令
        /// </summary>
        public bool OpenCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.6", value);
        }

        /// <summary>
        /// 合分命令
        /// </summary>
        public bool CloseOpenCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX1.7", value);
        }

        /// <summary>
        /// 分合命令
        /// </summary>
        public bool OpenCloseCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX2.0", value);
        }

        /// <summary>
        /// 分合分命令
        /// </summary>
        public bool OpenCloseOpenCommand(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX2.1", value);
        }

        /// <summary>
        /// 急停(主电源)
        /// </summary>
        public bool EmergencyStop(bool value)
        {
            return _plcCommunicator.WriteData("DB1.DBX2.2", value);
        }

        #endregion

        #region DB9 读取方法（监控数据）

        /// <summary>
        /// 读取DC电源输出电压值
        /// </summary>
        public ushort? ReadDCOutputVoltage()
        {
            var obj = _plcCommunicator.ReadData("DB9.DBW0");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取AC电源输出电压值
        /// </summary>
        public ushort? ReadACOutputVoltage()
        {
            var obj = _plcCommunicator.ReadData("DB9.DBW2");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取AC电源输出频率值
        /// </summary>
        public ushort? ReadACOutputFrequency()
        {
            var obj = _plcCommunicator.ReadData("DB9.DBW4");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取直流电机输出电压值
        /// </summary>
        public ushort? ReadDCMotorOutputVoltage()
        {
            var obj = _plcCommunicator.ReadData("DB9.DBW6");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        #endregion

        #region 相接触点状态读取方法（单独读取每个点）

        /// <summary>
        /// 读取A相接触点状态 (DI0.4)
        /// </summary>
        public ushort? ReadPhaseAContactStatus()
        {
            var obj = _plcCommunicator.ReadData("DI0.4");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取B相接触点状态 (DI0.5)
        /// </summary>
        public ushort? ReadPhaseBContactStatus()
        {
            var obj = _plcCommunicator.ReadData("DI0.5");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取C相接触点状态 (DI0.6)
        /// </summary>
        public ushort? ReadPhaseCContactStatus()
        {
            var obj = _plcCommunicator.ReadData("DI0.6");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        #endregion

        #region 相合闸分闸状态读取方法

        /// <summary>
        /// 读取A相合闸状态 (DI1.1) - 常闭触点
        /// </summary>
        public ushort? ReadPhaseACloseStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.1");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取A相分闸状态 (DI1.2) - 常开触点
        /// </summary>
        public ushort? ReadPhaseAOpenStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.2");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取B相合闸状态 (DI1.3) - 常闭触点
        /// </summary>
        public ushort? ReadPhaseBCloseStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.3");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取B相分闸状态 (DI1.4) - 常开触点
        /// </summary>
        public ushort? ReadPhaseBOpenStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.4");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取C相合闸状态 (DI1.5) - 常闭触点
        /// </summary>
        public ushort? ReadPhaseCCloseStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.5");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        /// <summary>
        /// 读取C相分闸状态 (DI1.6) - 常开触点
        /// </summary>
        public ushort? ReadPhaseCOpenStatus()
        {
            var obj = _plcCommunicator.ReadData("DI1.6");
            if (null == obj)
            {
                return null;
            }
            return (ushort)obj;
        }

        #endregion
    }
}